package 笔试.秋招2023.同盾科技.T1;

import java.util.Scanner;

public class Main {
    public static int count;
    public static int root;
    public static int[][] tree;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        count = sc.nextInt();//总个数
        root = sc.nextInt();
        tree = new int[count][3];
        for (int i = 0; i < count; i++) {
            tree[i][0] = sc.nextInt();
            tree[i][1] = sc.nextInt();//L
            tree[i][2] = sc.nextInt();//R
        }
        int lookFor = sc.nextInt();


        getNode(lookFor);

        System.out.println(0);
    }

    public static void getNode(int lookFor){
        for (int i = 0; i < count; i++) {
            //存在 右子节点
            if (lookFor == tree[i][0]&&tree[1][0]!=0) {
                System.out.println(tree[i][2]);
                return;
            }
        }
        //无右节点，找上一层的右节点
        for (int i = 0; i < count; i++) {
            int father = tree[i][1];
            if (lookFor == father) {
                System.out.println(tree[i][0]);
            }else {
                getNode(father);
            }
        }

    }
}
